package com.tal.live.config;

import com.tal.live.annotation.ApiVersion;
import com.tal.live.annotation.ApiVersionConstant;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Arrays;

/**
 * Swagger2配置类
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {

    @Bean
    public Docket CreateRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo(ApiVersionConstant.TEST_V1))
                .groupName(ApiVersionConstant.TEST_V1)
                .select()
                //.apis(RequestHandlerSelectors.basePackage("com.tal.live.*.controller"))
                .apis(input -> {
                            ApiVersion apiVersion = input.getHandlerMethod().getMethodAnnotation(ApiVersion.class);
                            if(apiVersion!=null&& Arrays.asList(apiVersion.group()).contains(ApiVersionConstant.TEST_V1)){
                                return true;
                            }
                            return false;
                        }
                )
                .paths(PathSelectors.any())
                .build();
    }


    @Bean
    public Docket vApp10() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo(ApiVersionConstant.QING_LUAN_V1))
                .groupName(ApiVersionConstant.QING_LUAN_V1)
                .select()
                //.apis(RequestHandlerSelectors.basePackage("com.tal.live.*.controller"))
                .apis(input -> {
                    ApiVersion apiVersion = input.getHandlerMethod().getMethodAnnotation(ApiVersion.class);
                    if(apiVersion!=null&& Arrays.asList(apiVersion.group()).contains(ApiVersionConstant.QING_LUAN_V1)){
                        return true;
                    }
                    return false;
                })//controller路径
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo(String version) {
        return new ApiInfoBuilder()
                .title("tal-transfer pass化 中转服务中心")
                .version(version)
                .contact("xianliru@100tal.com")
                .description("中转服务API")
                .termsOfServiceUrl("http://localhost:9081/")
                .build();
    }
}
